<style scoped>
.mask{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
    z-index: 9;
}
._ReviSion{
    position: fixed;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    width: 1220px;
    height: 500px;
    z-index: 99;
    background-color: white;
    overflow: auto;
}
.RS_top{
    height: 40px;
    background: #f8f8f8;
    display: flex;
    justify-content: space-between;
    padding: 10px 15px;
}
.RS_top p{
    font-weight: 700;
}
.RS_middle{
    width: 1190px;
    margin: 10px auto;
}
.button_box{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0;
    display: flex;
    padding-left: 15px;
}
.inp_box2{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0 15px 10px;
    margin: 15px 0;
    display: flex;
}
.inp_box{
    display: flex;
    margin: 15px 0 10px 0;
    transform: translateX(-20px);
}
.inp_box p {
  display: inline-block;
  width: 70px;
  text-align: right;
  white-space: nowrap;
  line-height: 30px;
  margin-right: 5px;
}
.inp_box2 p {
  margin: 0 5px;
  line-height: 30px;
}
.vendorStockQuery input:focus{
    outline: none;
    border: 1px solid #2a8ffc;
  }
  .inp{
    border: 1px solid #dbdbdb;
    border-radius: 3px;
    height: 30px;
    padding: 0 5px;
  }
  .inp2{
    width: 160px;
  }
  .inp3{
    width: 50px;
    margin-right: 10px;
  }
  .inp4{
      width: 411px;
  }
  .jl1{
      margin:2px 6px 0 0;
  }
  .jl{
      margin: 2px 6px 0 10px;
  }
  .jl2{
      margin: 2px 6px 0 41px;
  }
  .RS_button{
      margin-left: 1077px;
  }
  .largeSelect {
    width: 160px;
  }
</style>

<style>
.viUpdate .el-input, .viUpdate .el-input__inner {
	height: 30px!important;
	width: 160px;
}
.viUpdate .minSelect, .viUpdate .minSelect .el-input__inner {
  width: 100px;
  height: 30px;
}
</style>

<template>
  <div class="viUpdate">
    <div class="mask"></div>
    <div class="_ReviSion">
      <div class="RS_top">
        <p>改单(厂商进货)</p>
        <i @click="closeReviSion" class="el-icon-close"></i>
      </div>
      <div class="RS_middle">
        <div class="button_box">
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button icon="el-icon-edit-outline" @click="modification(0)">修改</el-button>
          <!-- <el-button icon="el-icon-edit-outline" @click="() => {this.isNew = true}">new</el-button> -->
        </div>
        <div class="inp_box">
          <p>单号</p>
          <input class="inp inp2" type="text" v-model="odd">
          <p>收货仓库</p>
          <input class="inp inp3" type="text" v-model="inputWarehouseNo" @blur="inputWarehouseNoBlur" :disabled="!isNew">
          <el-select v-model="inputWarehouseName" @change="warehouseChange1" :disabled="!isNew" class="minSelect">
            <el-option v-for="item in warehouseList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <p>公司仓库</p>
          <input class="inp inp3" type="text" v-model="companyNo" @blur="companyNoBlur" :disabled="!isNew">
          <el-select v-model="companyName" @change="warehouseChange2" :disabled="!isNew" class="minSelect">
            <el-option v-for="item in warehouseList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <p>厂商编号</p>
          <input class="inp inp3" type="text" v-model="manufacturerNo" @blur="manufacturerNoBlur" :disabled="!isNew">
          <el-select v-model="manufacturerName" @change="manufacturerChange" :disabled="!isNew" class="minSelect">
            <el-option v-for="item in manufacturerList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <p>进货日期</p>
          <el-date-picker class="inp2" v-model="inputDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" disabled></el-date-picker>
        </div>
        <div class="inp_box">
          <p>货号</p>
          <el-input v-model="kindsNumber" disabled></el-input>
          <p>数量</p>
          <el-input v-model="sum" @blur="numberBlur" :disabled="!dataSource"></el-input>
          <p>配货方式</p>
          <el-select v-model="cooperateWay" class="largeSelect">
            <el-option v-for="item in cooperateWayList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <p>厂进序号</p>
          <el-input v-model="inputSequence" disabled></el-input>
          <p>备注</p>
          <el-input v-model="remark"></el-input>
        </div>
        <div class="inp_box2">
          <p>货号</p>
          <div class="sty3">
            <el-input v-model="searchKindsNumber" @keyup.enter.native="searchKN"></el-input>
          </div>
          <p>厂进序号</p>
          <div class="sty3">
            <el-input v-model="searchInputSequence" @keyup.enter.native="search"></el-input>
          </div>
        </div>
        <el-table height="150" :data="tableData" highlight-current-row :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" :cell-style="{padding:0,height:'36px'}" style="width: 100%" @row-click="rowClick">
          <el-table-column type="index" width="50" label="序号"></el-table-column>
          <el-table-column prop="kindsNumber" label="货号" width="160"></el-table-column>
          <el-table-column prop="colorName" label="颜色" width="100"></el-table-column>
          <el-table-column prop="sizeNo" width="80" label="尺码编号"></el-table-column>
          <el-table-column prop="sum" label="数量" width="80"></el-table-column>
          <el-table-column prop="sellingPrice" label="售价" width="80"></el-table-column>
          <el-table-column prop="sellingPriceMoney" label="售价金额" width="100"></el-table-column>
          <el-table-column prop="remark" label="备注"></el-table-column>
          <el-table-column prop="cooperateWay" label="配货方式" width="80"></el-table-column>
          <el-table-column prop="brandedName" label="品牌" width="100"></el-table-column>
          <el-table-column prop="sellingPriceMoney" label="金额" width="80"></el-table-column>
          <el-table-column prop="enteringTime" label="录入时间" width="120"></el-table-column>
        </el-table>
      </div>
      <el-button @click="closeReviSion" class="RS_button">取消</el-button>
      <el-button type="primary" @click="modification(1)">保存</el-button>
    </div>
  </div>
</template>


<script>
import { inputQuery, modifySequence, factoryInput } from '../../../../network/manufacturer';
import today from '../../../../components/mixins/today'

export default {
  mixins: [today],
  props: {
    selectInputSequence: {
      type: String,
      default: ''
    },
    manufacturerL: {
      type: Array,
      default: []
    },
    warehouseL: {
      type: Array,
      default: []
    },
    showReviSion: {
      type: String,
      default: 'none'
    },
    Str: {
      type: String,
      default: ''
    }
  },

  data(){
    return{
      dataSource: 1,
      odd: '',
      warehouseList: [],
      inputWarehouseNo: '',
      inputWarehouseName: '',
      companyNo: '',
      companyName: '',
      manufacturerNo: '',
      manufacturerName: '',
      manufacturerList: [],
      inputDate: '',
      kindsNumber: '',
      sum: '',
      cooperateWay: '配货',
      cooperateWayList: [
        {value:'配货',label:'配货'},
        {value:'订货',label:'订货'},
        {value:'订断',label:'订断'},
        {value:'追断',label:'追断'}
      ],
      inputSequence: '',
      remark: '',

      searchKindsNumber: '',
      searchInputSequence: '',
      
      tableData: [],
      allData: [],
      updateData: {},

      changeNo: false,
      lockNo: false,
      isNew: true
    }
  },
  watch: {
    Str(a,b){
      console.log(a)
    },
    selectInputSequence(a,b){
      this.searchInputSequence = a
    },
    showReviSion(a,b){
      if (a == 'block' && this.searchInputSequence) {
        this.search()
      }
    },
    manufacturerL(a,b){
      this.manufacturerList = a
    },
    warehouseL(a,b){
      this.warehouseList = a
    },
    odd(a,b){
      this.odd = a.toUpperCase().trim()
      if (this.tableData.length && a != this.tableData[0].odd && !this.lockNo) {
        this.lockNo = true
      }
    },
    // inputDate(a,b){
    //   let tmp = a.slice(0,10)
    //   if (this.tableData.length && tmp != this.tableData[0].purchaseTime && !this.lockNo) {
    //     this.lockNo = true
    //   }
    // },
    cooperateWay(a,b){
      if (this.tableData.length && a != this.tableData[0].cooperateWay && !this.lockNo) {
        this.lockNo = true
      }
    },
    inputWarehouseNo(a,b){
      this.inputWarehouseNo = a.toUpperCase().trim()
      if (this.tableData.length && a != this.tableData[0].gatherCargoWarehouse && !this.lockNo) {
        this.lockNo = true
      }
    },
    companyNo(a,b){
      this.companyNo = a.toUpperCase().trim()
      if (this.tableData.length && a != this.tableData[0].warehouseNo && !this.lockNo) {
        this.lockNo = true
      }
    },
    manufacturerNo(a,b){
      this.manufacturerNo = a.toUpperCase().trim()
      if (this.tableData.length && a != this.tableData[0].manufacturerNo && !this.lockNo) {
        this.lockNo = true
      }
    },
    searchKindsNumber(a,b){
      this.searchKindsNumber = a.toUpperCase().trim()
    },
    searchInputSequence(a,b){
      this.searchInputSequence = a.toUpperCase().trim()
    }
  },
  methods:{
    clearData(){
      this.odd = ''
      this.inputWarehouseNo = ''
      this.inputWarehouseName = ''
      this.companyNo = ''
      this.companyName = ''
      this.manufacturerNo = ''
      this.manufacturerName = ''
      this.inputDate = ''
      this.kindsNumber = ''
      this.sum = ''
      this.cooperateWay = '配货'
      this.searchKindsNumber = ''
      // this.searchInputSequence = ''
      this.tableData = []
      this.allData = []
      this.lockNo = false
      this.changeNo = false
      // this.isNew = false
      // this.isNew = true
    },
    closeReviSion(){
      this.$emit('closeReviSion')
      this.clearData()
    },

    // 厂商编号，仓库，失去焦点
    manufacturerNoBlur(){
      if (this.manufacturerNo != '') {
        for (const i of this.manufacturerList) {
          if (this.manufacturerNo == i.value) {
            this.manufacturerName = i.label
            return
          }
        }
        this.$alert('无此厂商编号', '', {
          confirmButtonText: '确定'
        })
        this.manufacturerNo = ''
        this.manufacturerName = ''
      }
    },
    inputWarehouseNoBlur(){
      if (this.inputWarehouseNo != '') {
        for (const i of this.warehouseList) {
          if (this.inputWarehouseNo == i.value) {
            this.inputWarehouseName = i.label
            return
          }
        }
        this.$alert('无此仓库编号', '', {
          confirmButtonText: '确定'
        })
        this.inputWarehouseNo = ''
        this.inputWarehouseName = ''
      }
    },
    companyNoBlur(){
      if (this.companyNo != '') {
        for (const i of this.warehouseList) {
          if (this.companyNo == i.value) {
            this.companyName = i.label
            return
          }
        }
        this.$alert('无此仓库编号', '', {
          confirmButtonText: '确定'
        })
        this.companyNo = ''
        this.companyName = ''
      }
    },
    warehouseChange1(e){
      this.inputWarehouseNo = e
      if (this.tableData.length && this.companyNo != this.tableData[0].gatherCargoWarehouse && !this.lockNo) {
        this.tableData.forEach(e => {
          e.gatherCargoWarehouse = this.companyNo
        })
      }
    },
    warehouseChange2(e){
      this.companyNo = e
      if (this.tableData.length && this.companyNo != this.tableData[0].warehouseNo && !this.lockNo) {
        this.tableData.forEach(e => {
          e.warehouseNo = this.companyNo
        })
      }
    },
    manufacturerChange(e){
      this.manufacturerNo = e
      // for(let b of this.manufacturerList){
      //   if(e == b.value){
      //     this.discount = b.discount
      //   }
      // }
      if (this.tableData.length && this.manufacturerNo != this.tableData[0].manufacturerNo) {
        this.tableData.forEach(e => {
          e.manufacturerNo = this.manufacturerNo
        })
      }
    },

    numberBlur(){
      let a = this.sum
      let r = /^(0|[1-9]\d*)$/
      if (!r.test(a)) {
        this.$alert('数量请输入整数', '', {
          confirmButtonText: '确定'
        })
        this.sum = this.updateData.sum
        return
      }
      if (this.tableData.length && a != this.tableData[0].sum && !this.lockNo) {
        this.lockNo = true
      }
      this.updateData.sum = a
      this.updateData.sellingPriceMoney = (this.updateData.sum * this.updateData.sellingPrice).toFixed(2)
    },

    fillBlanks(row){
      this.inputSequence = row.sequence
      this.inputDate = row.purchaseTime.slice(0,10) + ' 12:00:00'
      this.odd = row.receiptsNumber
      this.remark = row.remark
      this.cooperateWay = row.cooperateWay

      this.manufacturerNo = row.manufacturerNo
      this.inputWarehouseNo = row.gatherCargoWarehouse
      this.companyNo = row.warehouseNo

      this.manufacturerNoBlur()
      this.inputWarehouseNoBlur()
      this.companyNoBlur()
    },
    rowClick(row){
      // console.log(row)
      this.kindsNumber = row.kindsNumber
      this.sum = row.sum

      this.updateData = row
    },

    // 查询
    search(){
      if (!this.searchInputSequence) {
        this.$alert('请输入厂进序号查询', '', {
          confirmButtonText: '确定'
        })
        return
      }
      inputQuery(JSON.stringify({
        sequence: this.searchInputSequence,
        pageData: {
          page: 1,
          limit: 999
        }
      })).then(res => {
        if (res.data.code == 200) {
          let obj = res.data.data.resultList[0]
          this.dataSource = obj.dataSource
          this.allData = obj.detailVOList || []
          this.allData.forEach(e => {
            e.knInfo = e.kindsNumber
            e.kindsNumber = e.knInfo.kindsNumber
            e.colorName = e.knInfo.colorName
            e.colorNo = e.knInfo.colorNo
            e.size = e.knInfo.size
            e.sizeNo = e.knInfo.sizeNo
            e.commodityName = e.knInfo.commodityName
            e.brandedName = e.knInfo.brandedName
            e.purchaseTime = obj.purchaseTime.slice(0,10)
          })
          this.tableData = this.allData
          this.fillBlanks(obj)
        }
      })
    },

    modification(f){
      if (!this.inputWarehouseNo) {
        this.$alert('收货仓库不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.companyNo) {
        this.$alert('公司仓库不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.manufacturerNo) {
        this.$alert('厂商不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.inputDate) {
        this.$alert('进货日期不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }

      let kindsNumberNumberMap = {}
      this.allData.forEach(e => {
        e.purchaseTime = this.inputDate.slice(0,10)
        e.manufacturerNo = this.manufacturerNo
        e.receiptsNumber = this.odd
        e.gatherCargoWarehouse = this.inputWarehouseNo
        e.warehouseNo = this.companyNo
        e.cooperateWay = this.cooperateWay
        e.remark = this.remark
        kindsNumberNumberMap[e.kindsNumber] = e.sum
        e.flagToken = this.Str
      })

      // if (this.isNew) {
      //   // 新厂进
      //   let d = {
      //   manufacturerDTO: {
      //     dataSource: 1,
      //     isNewBox: true,
      //     isKindsNumber: true,
      //     inputSequence: '',
      //     inputDate: this.inputDate,
      //     manufacturerNo : this.manufacturerNo,
      //     odd : this.odd,
      //     inputWarehouseNo : this.inputWarehouseNo,
      //     companyNo : this.companyNo,
      //     cooperateWay : this.cooperateWay,
      //     kindsNumberNumberMap,
      //     kindsNumberMap: {},
      //     discount: 10
      //   },
      //   rfidList: []
      // }
      // // console.log(d)
      // factoryInput(JSON.stringify(d)).then(res => {
      //   if (res.data.code == 200) {
      //     this.$message({
      //       message: '新建成功',
      //       type: 'success'
      //     })
      //   } else {
      //     this.$message.error('新建失败')
      //   }
      // })
      // } else {
        console.log(this.allData)
        modifySequence(JSON.stringify(this.allData)).then(res => {
          if (res.data.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message.error('修改失败')
          }
        })
      // }
      console.log(f)
      if (f) {
        this.closeReviSion()
      }
    },

    searchKN(){
      if (this.searchKindsNumber) {
        this.tableData = []
        this.allData.forEach(e => {
          if (this.searchKindsNumber == e.kindsNumber) {
            this.tableData.push(e)
          }
        })
      } else {
        this.tableData = this.allData
      }
    }
  }
}
</script>